<div class="layui-fluid">
    <div class="layui-breadcrumb jy-breadcrumb">

    </div>
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="LAY-list-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">SKU</label>
                    <div class="layui-input-block">
                        <input type="text" name="skus" placeholder="多个用空格或逗号隔开" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">SPU</label>
                    <div class="layui-input-block">
                        <input type="text" name="spus" placeholder="多个用空格或逗号隔开" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn search" lay-submit lay-filter="LAY-list-search">查询</button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <div class="layui-form" lay-filter="batchOp-form">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <select lay-filter="batchOp" >
                            <option value="0" selected>批量操作</option>
                            <option value="1">导入数据</option>
                        </select>
                    </div>
                </div>
            </div>
            <table id="LAY-list-normal"  lay-filter="LAY-list-normal"></table>
            <!-- 行内工具 -->
            <script type="text/html" id="operation">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" href="javascript:;">编辑</a>
            </script>
        </div>
    </div>
</div>
<script type="text/html" id="assign">
    <div class="layui-card layui-form" lay-filter="LAY-list-form3">
        <div class="layui-form-item" style='padding-top: 10px'>
            <label class="layui-form-label">SKU：</label>
            <div class="layui-input-block">
                <input type="text" name="sku" id="skuEdit" value="{{d.sku||''}}" autocomplete="off" placeholder="" class="layui-input gray" style="width:180px" disabled=disabled>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">编辑人员：</label>
            <div class="layui-input-block" style="padding: 0px 0px 10px 0px">
                <select name="edit_operator" xm-select="edit_operator" xm-select-search="/index/User/getUserByName" xm-select-radio=""></select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">图片人员：</label>
            <div class="layui-input-block" style="padding: 0px 0px 10px 0px">
                <select name="img_operator" xm-select="img_operator" xm-select-search="/index/User/getUserByName" xm-select-radio=""></select>
            </div>
        </div>
    </div>
</script>
<script>
    layui.use(['admin', 'table', 'form', 'toolJ', 'formSelects','upload'], function () {
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , upload = layui.upload
            , formSelects = layui.formSelects
            , toolJ = layui.toolJ
            , form = layui.form;

        form.render(null);
        layui.$("div[lay-filter='batchOp-form'] dd[lay-value=0]").remove();
        
        //批量操作
        form.on('select(batchOp)', function(data){
            if(data.value == 1) {
                var url = '/index/Editor/uploadSkuEditorInfo';
                var template = '/attachment/importcvs/skueditor.csv'; 
                toolJ.upload(url, template);
            }
            form.render(null, 'batchOp-form');
        });

        viewTable();
        layui.form.on('submit(LAY-list-search)', function (data) {
            var field = data.field;
            viewTable(field);
        });

        function viewTable(field = {}) {
            param = [
                {field: 'sku', title: 'SKU'}
                , {field: 'spu', title: 'SPU'}
                , {field: 'edit_operator_name', title: '编辑人员'}
                , {field: 'img_operator_name', title: '图片人员'}
                , {field: 'update_time', title: '最后操作时间'}
                , {fixed: 'right', title:'操作', toolbar: '#operation', width:70}
            ];

            table.render({
                elem: "#LAY-list-normal"
                , url: '/index/Editor/getEditorList'
                , where: field
                , cols: [param]
                , page: true
                , limit: 50
                ,text:{none: '暂无相关数据!' }
            });

            //监听行内操作
            table.on('tool(LAY-list-normal)', function(obj){
                // debugger;
                var data = obj.data;
                if(obj.event == 'edit') {
                    layer.open({
                        type: 5,
                        title: '编辑',
                        shade: [0.5, '#000'],
                        offset: 'auto',
                        area: ['350px', '280px'],
                        shadeClose: true,
                        closeBtn: 1,
                        anim: 2,
                        id: 'LAY_assign',
                        btn: ['保存', '取消'],
                        btn1: function(index, layero){
                            var img_operator = $("input[name=img_operator]").val();
                            var edit_operator = $("input[name=edit_operator]").val();
                            var skuEdit = $("#skuEdit").val();
                            var obj = {};
                            obj.url = '/index/Editor/updateOperatorInfo';
                            obj.data ={sku:skuEdit,edit:edit_operator,img:img_operator};
                            toolJ.ajax(obj);
                            return false;
                        },
                        content: $('#assign').html(),
                        success: function(layero){
                            formSelects.data('img_operator', 'local', {
                                arr: [
                                    {"name": data.img_operator_name, "value": data.img_operator},
                                ]
                            });
                            formSelects.value('img_operator', [data.img_operator]);

                            formSelects.data('edit_operator', 'local', {
                                arr: [
                                    {"name": data.edit_operator_name, "value": data.edit_operator},
                                ]
                            });
                            formSelects.value('edit_operator', [data.edit_operator]);
                        },
                    });
                    toolJ.assign(data, '.layui-form');
                }
            });
        }
    });
</script>